//
//  News.m
//  YiLiao
//
//  Created by mac on 16/7/1.
//  Copyright © 2016年 Noah. All rights reserved.
//

#import "News.h"
#import "FirstTableCell.h"
#import "SecondTableViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "Details.h"

@interface News ()<UITableViewDataSource,UITableViewDelegate>
{
    NSDictionary *allValueDic;
    NSArray *allValurArray;
    NSArray *allArray;
}
@property (nonatomic ,strong)UITableView *tableView;
@end

@implementation News
-(UITableView *)tableView{
    if (!_tableView) {
        self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenHeight-20) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [_tableView registerNib:[UINib nibWithNibName:@"FirstTableCell" bundle:nil] forCellReuseIdentifier:@"cellA"];
        [_tableView registerNib:[UINib nibWithNibName:@"SecondTableViewCell" bundle:nil] forCellReuseIdentifier:@"cellB"];
        _tableView.rowHeight=200;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    [self.view addSubview:self.tableView];
    self.navigationItem.title=@"新闻中心";
    
    [self getData];
    
}
-(void)getData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
     manager .responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/plain", nil]];

    
    [manager GET:@"http://localhost:8080/st/news/news_list.json" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *infoDic=(NSDictionary *)responseObject;
//        NSLog(@"%@",infoDic[@"news_list"]);
        allValurArray=infoDic[@"news_list"];
        
        [_tableView reloadData];
//        NSLog(@"%@",allValueDic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
    }];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return allValurArray.count;

    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
  
//    NSURL *url=[NSURL URLWithString:@"http://localhost:8080"];
    NSDictionary *infoDic=allValurArray[indexPath.row];
    
    NSArray *imgArray=infoDic[@"images"];
    if (imgArray.count==1 || imgArray .count==2) {
        FirstTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cellA" forIndexPath:indexPath];
        cell.title.text=infoDic[@"news_title"];
        cell.intro.text=infoDic[@"intro"];
        cell.channel.text=infoDic[@"source"];
        cell.channel.adjustsFontSizeToFitWidth=YES;
        [cell.UrlImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",imgArray[0][@"url"]]]];
       
      return cell;
    
       
    }else{
        SecondTableViewCell *cell1=[tableView dequeueReusableCellWithIdentifier:@"cellB" forIndexPath:indexPath];
        cell1.TitleText.text=infoDic[@"news_title"];
        cell1.soure.text =infoDic[@"source"];
        cell1.soure.adjustsFontSizeToFitWidth=YES;
        [cell1.img1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",imgArray[0][@"url"]]]];
        [cell1.img2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",imgArray[1][@"url"]]]];
        if (imgArray.count==3) {
            [cell1.img3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080%@",imgArray[2][@"url"]]]];
            
        }
        return cell1;
    }
    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
    Details *VC=[[Details alloc]init];
    VC.soureURL =[allValurArray[indexPath.row] objectForKey:@"source_url"];
    [self.navigationController pushViewController:VC animated:YES];
}








































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
